package com.ihome.android.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.Enumeration;
import java.util.Random;

import android.content.Context;
import android.text.format.Formatter;
import android.util.Log;

public class Tools {
	/**
	 * 数字格式化， 每三个数字加个逗号
	 * 0,000,000
	 */
    public static String formatNumber(String num){
    	String result = "";
    	
    	if( num == null ){
    		return "0";
    	}
    	
    	if( num.length() > 3){
        	DecimalFormat df = new DecimalFormat();
        	String style = "0,000";
        	df.applyPattern(style);
        	
        	long data = Long.parseLong(num);
        	result = df.format(data);    		
    	} else {
    		result = num;
    	}
    	
    	return result;
    }
    
	/**
	 * 两个Double数相除，并保留scale位小数
	 * 
	 * @param v1
	 * @param v2
	 * @param scale
	 * @return Double
	 */
	public static Double divide(Double v1, Double v2, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		BigDecimal b1 = new BigDecimal(v1.toString());
		BigDecimal b2 = new BigDecimal(v2.toString());
		return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
	
	/**
	 * 两个Double数相除，并保留scale位小数
	 * 
	 * @param v1
	 * @param v2
	 * @param scale
	 * @return Double
	 */
	public static Long divide(Long v1, Long v2, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		BigDecimal b1 = new BigDecimal(v1.toString());
		BigDecimal b2 = new BigDecimal(v2.toString());
		return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).longValue();
	}
	
	/**
	 * 系统函数，字符串转换 long -String (kb)
	 * 
	 */
	public static String formateFileSize(Context context, long size) {
		return Formatter.formatFileSize(context, size);
	}
    
    /**
     * 获取一个随机数
     * @param iFirst 取值范围最小值, 只在判断随机数不等于零时有用
     * @param iEnd  取值范围最大值
     * @return
     */
    public static int getRandom( int iFirst, int iEnd) {
    	int iResult = 0;
    	
    	Random random = new Random();
    	iResult = random.nextInt(iEnd);
    	
    	if ( iFirst > 0 && iResult == 0 ) {
    		//5是随意设的值
    		iResult += 5;
    	}
    	
    	return iResult;
    }
	
	   /**
     * get local IPAddress
     * @return IP Address/or null if doesn't get any
     */
    public static String getLocalIpAddress() {   
        try {   
            for (Enumeration<NetworkInterface> en = NetworkInterface   
                    .getNetworkInterfaces(); en.hasMoreElements();) {   
                NetworkInterface intf = en.nextElement();   
                for (Enumeration<InetAddress> enumIpAddr = intf   
                        .getInetAddresses(); enumIpAddr.hasMoreElements();) {   
                    InetAddress inetAddress = enumIpAddr.nextElement();   
                    if (!inetAddress.isLoopbackAddress()) {   
                        return inetAddress.getHostAddress().toString();   
                    }   
                }   
            }   
        } catch (SocketException ex) {   
            Log.e("WifiPreference IpAddress", ex.toString());   
        }   
        return null;   
    } 
    
    public static String encode2UTF(String szSrc){
    	try {
			return URLEncoder.encode(URLEncoder.encode(szSrc, "utf-8"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
}
